package com.game.server.util;

import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.dao.XyClubInfoDao;
import com.game.server.db.dao.XyClubMemberInfoDao;
import com.game.server.db.dao.XyNotificationLogDao;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.db.entity.XyNotificationLog;
import com.game.server.db.entity.XyUnionInfo;
import com.game.server.message.proto.out.XYNotifyPartnersokRsp;
import com.game.server.message.proto.out.XYmailOperationRsp;
import net.sf.json.JSONObject;


//处理并发的工具类

public class ConcurrentUtil {

    //单例
    private static ConcurrentUtil instance;

    public static ConcurrentUtil getInstance() {
        if (instance == null) instance = new ConcurrentUtil();
        return instance;
    }

    public synchronized XYmailOperationRsp InformMessageDispose(LogicClient client, int type, int mid, XYmailOperationRsp rsp) {

        if (type == 1 || type == 2 || type == 3 || type == 4) {
            rsp.setIsok(2);
            XyNotificationLog log = XyNotificationLogDao.findById(mid);
            if (log.getNotification_type() == 1 && log.msg_type == 1) {//俱乐部 申请加入邮件
                XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(log.getCid());
                XyClubMemberInfo clubMemberInfo = clubInfo.getPlayer().get(client.getUserId());
                if (clubInfo != null && clubMemberInfo != null && clubMemberInfo.getIsManager() < 3) {//管理员  创建可操作
                    XyClubMemberInfo toClubMemberInfo = clubInfo.getPlayer().get(log.getUid());
                    if (toClubMemberInfo == null) {//被操作的用户不在俱乐部人员表内
                        if (type == 2 && log.getState() == 0) {
                            log.setState(2);
                            rsp.setIsok(1);
                            XyNotificationLogDao.updateState(log);
                        }
                        if (type == 1 && log.getState() == 0) {
                            log.setState(1);
                            XyClubMemberInfo newplayer = new XyClubMemberInfo();
                            newplayer.setCid(clubInfo.getId());
                            newplayer.setUid(log.getUid());
                            newplayer.setIsManager(3);
                            String additional_data = log.getAdditional_data();
                            newplayer.setPid(0);
                            //判断是否是通过上级合伙人搜索进入的俱乐部
                            if (additional_data != null && !"".equals(additional_data)) {
                                JSONObject jsonObject = JSONObject.fromObject(additional_data);
                                if (jsonObject != null) {
                                    if (jsonObject.get("searchType") != null) {
                                        int searchType = jsonObject.getInt("searchType");
                                        int searchid = jsonObject.getInt("searchid");
                                        if (searchType == 2 && searchid != 0) {
                                            XyClubMemberInfo byPartnerId = clubInfo.findByPartnerId(searchid);
                                            if (byPartnerId != null) {
                                                newplayer.setPid(byPartnerId.getUid());
                                            }
                                        }
                                    }
                                }
                            }
                            long clubMemberid = XyClubMemberInfoDao.insertInto(newplayer);
                            newplayer = XyClubMemberInfoDao.findById(clubMemberid);
                            clubInfo.addPlayer(newplayer);
                            //将俱乐部ID添加到缓存中
                            DataManager.getInstance().getMapUserData().get(newplayer.getUid()).getClubids().add(clubInfo.getId());
                            rsp.setIsok(1);
                            XyNotificationLogDao.updateState(log);
                        }
                    } else {
                        if (type == 2 && log.getState() == 0) {
                            log.setState(2);
                            rsp.setIsok(1);
                            XyNotificationLogDao.updateState(log);
                        }
                        if (type == 1 && log.getState() == 0) {
                            if (toClubMemberInfo.getDel_type() == 1) {
                                log.setState(1);
                                int deleteClubMember = XyClubMemberInfoDao.deleteClubMember(toClubMemberInfo.getId(), 0);
                                if (deleteClubMember > 0) {
                                    toClubMemberInfo.setDel_type(0);
                                    //将俱乐部ID添加到缓存中
                                    DataManager.getInstance().getMapUserData().get(toClubMemberInfo.getUid()).getClubids().add(clubInfo.getId());
                                    rsp.setIsok(1);
                                    XyNotificationLogDao.updateState(log);
                                }
                            }
                        }
                    }
                }
            }

            if (log.getNotification_type() == 1 && log.msg_type == 2) {//俱乐部合伙人 申请邮件
                XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(log.getCid());
                XyClubMemberInfo clubMemberInfo = clubInfo.getPlayer().get(client.getUserId());
                if (clubInfo != null && clubMemberInfo != null && clubMemberInfo.getIsManager() < 3) {
                    XyClubMemberInfo toClubMemberInfo = clubInfo.getPlayer().get(log.getUid());
                    if (type == 4 && log.getState() == 0) {
                        log.setState(2);
                        rsp.setIsok(1);
                        XyNotificationLogDao.updateState(log);
                    }
                    if (type == 3 && log.getState() == 0) {//同意成为合伙人
                        log.setState(1);
                        int updateCount = XyClubMemberInfoDao.updeteIsAccessory(clubInfo.getId(), toClubMemberInfo.getUid(), 1);
                        if (updateCount > 0) {
                            toClubMemberInfo.setIsAccessory(1);
                            int xyclubid = 0;
                            while (true) {//获取一个未使用的ID
                                xyclubid = DataManager.getInstance().findOnlyID("xy_club_info", "id");
                                int count = XyClubMemberInfoDao.findPartnerIdCount(xyclubid);
                                if (count != 0) {
                                    break;
                                }
                            }
                            toClubMemberInfo.setPartner_id(xyclubid);
                            XyClubMemberInfoDao.updetePartnerId(clubInfo.getId(), toClubMemberInfo.getUid(), xyclubid);
                            rsp.setIsok(1);
                            XyNotificationLogDao.updateState(log);

                            //同意用户成为合伙人后 如果用户还在俱乐部则 通知对方修改俱乐部权限信息
                            LogicClient toClient = (LogicClient) LogicServer.getInstance().getClient(toClubMemberInfo.getUid());
                            if (toClient != null && toClient.getClubid() == clubInfo.getId()) {
                                XYNotifyPartnersokRsp xyNotifyPartnersokRsp = new XYNotifyPartnersokRsp();
                                xyNotifyPartnersokRsp.setIsPartner(toClubMemberInfo.getIsAccessory());
                                xyNotifyPartnersokRsp.setPartnerId(toClubMemberInfo.getPartner_id());
                                toClient.Send(xyNotifyPartnersokRsp);
                            }
                        }
                    }
                }
            }
        }

        if (type == 5 || type == 6) {
            XyNotificationLog log = XyNotificationLogDao.findById(mid);
            if (log != null && log.getState() == 0 && log.getNotification_type() == 2 && log.msg_type == 3) {
                XyClubInfo xyClubInfo = DataManager.getInstance().getMapClub().get(log.getUid());

                if (type == 6) {
                    log.setState(2);
                    rsp.setIsok(1);
                    XyNotificationLogDao.updateState(log);
                }
                if (type == 5) {
                    log.setState(1);
                    rsp.setIsok(1);
                    XyNotificationLogDao.updateState(log);
                    if (xyClubInfo != null && xyClubInfo.getUnionid() == 0) {
                        XyUnionInfo unionInfo = DataManager.getInstance().getMapUnion().get(log.getCid());
                        if (unionInfo != null) {
                            XyClubInfoDao.updateUnionId(xyClubInfo.getId(), unionInfo.getId());
                            xyClubInfo.setUnionid(unionInfo.getId());
                            unionInfo.getClubids().add(xyClubInfo.getId());
                        }
                    }
                }
            }
        }
        return  rsp;
    }
}
